import { resolve } from 'path'
//↑若报错“找不到模块“path”或其相应的类型声明”，npm i @types/node,然后重启vscode

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Inspect from 'vite-plugin-inspect'
// gzip压缩
import viteCompression from "vite-plugin-compression";
const pathSrc = resolve(__dirname, 'src')

export default defineConfig({
  server: {
    // host: '0.0.0.0',注意若为electron开发环境此项不能解开，否则检测不到tcp5173端口运行从而启动electron
  },
  base: './',
  build: {
    chunkSizeWarningLimit: 1000,
    // rollupOptions: {
    // 	output: {
    // 		//解决打包时Some chunks are larger警告
    // 		manualChunks(id) {
    // 			if (id.includes('node_modules')) {
    // 				return id.toString().split('node_modules/')[1].split('/')[0].toString();
    // 			}
    // 		}
    // 	}
    // }
  },
  plugins: [
    vue(),
    viteCompression({
      //打包压缩插件
      filter:/\.(js|mjs|json|css|html|jpg|png|jepg|gif)$/i,
      verbose: true,
      disable: false,
      threshold: 10240,
      algorithm: 'gzip',
      ext: ".gz",
      deleteOriginFile: false // 是否删除原始文件
    }),
    AutoImport({
      // Auto import functions from Vue, e.g. ref, reactive, toRef...
      // 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
      imports: ['vue'],

      // Auto import functions from Element Plus, e.g. ElMessage, ElMessageBox... (with style)
      // 自动导入 Element Plus 相关函数，如：ElMessage, ElMessageBox... (带样式)
      resolvers: [
        ElementPlusResolver(),

        // Auto import icon components
        // 自动导入图标组件
        IconsResolver({
          prefix: 'Icon',
        }),
      ],

      dts: resolve(pathSrc, 'auto-imports.d.ts'),
    }),
    Components({
      resolvers: [
        // Auto register icon components
        // 自动注册图标组件
        IconsResolver({
          enabledCollections: ['ep'],
        }),
        // Auto register Element Plus components
        // 自动导入 Element Plus 组件
        ElementPlusResolver(),
      ],

      dts: resolve(pathSrc, 'components.d.ts'),
    }),

    Icons({
      autoInstall: true,
    }),

    Inspect(),
  ],
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: '@import "./src/assets/scss/style.scss";' // 添加公共样式
      }
    },
    postcss: {
      plugins: [
        {
          postcssPlugin: 'internal:charset-removal',
          AtRule: {
            charset: (atRule) => {
              if (atRule.name === 'charset') {
                atRule.remove();
              }
            }
          }
        }
      ]
    }
  },

})


